package com.tuanzi.loan.business.service.family.impl;

import com.tuanzi.loan.business.entity.family.LoanApiOrder;
import com.tuanzi.loan.business.entity.family.LoanApiRequestRecord;
import com.tuanzi.loan.business.repository.family.LoanApiOrderRepository;
import com.tuanzi.loan.business.repository.family.LoanApiRequestRecordRepository;
import com.tuanzi.loan.business.service.family.LoanApiOrderService;
import com.tuanzi.loan.core.aspect.Debug;
import com.tuanzi.loan.core.vo.PageSearcher;
import com.tuanzi.loan.vo.request.family.LoanApiOrderSearchRequest;
import com.tuanzi.loan.vo.response.family.LoanApiOrderSearchResponse;
import com.tuanzi.loan.vo.view.family.LoanApiOrderInfoView;
import com.tuanzi.loan.vo.view.family.LoanApiRequestRecordView;
import com.tuanzi.server.loan_base.util.PhoneNumNideUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.retry.RetryException;
import org.springframework.retry.annotation.Retryable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;

@Debug
@Service
@Transactional
@Retryable(value = { RetryException.class })
public class LoanApiOrderServiceImpl implements LoanApiOrderService {

	@Autowired
	private LoanApiOrderRepository loanApiOrderRepository;
	@Autowired
	private LoanApiRequestRecordRepository requestRecordRepository;
	
	@Override
	public Long countByUserIdAndSourceId(Long userId, Long sourceId) {
		return loanApiOrderRepository.countByUserIdAndSourceId(userId, sourceId);
	}
	

	@Override
	public LoanApiOrderInfoView loanApiOrderInfoView(Long orderId) {

		LoanApiOrderInfoView loanOrderInfo = new LoanApiOrderInfoView();

		LoanApiOrder order = loanApiOrderRepository.getOne(orderId);

		if (null != order) {
			loanOrderInfo.setId(orderId);
			loanOrderInfo.setUserId(order.getUserId());
			loanOrderInfo.setMobnum(PhoneNumNideUtil.hideNum(order.getMobnum()));
			loanOrderInfo.setIdCard(PhoneNumNideUtil.hideIdCard(order.getIdCard()));
			loanOrderInfo.setPetname(PhoneNumNideUtil.hideName(order.getPetname()));
			loanOrderInfo.setAmount(order.getAmount());
			loanOrderInfo.setType(order.getType());
			loanOrderInfo.setStatus(order.getStatus());
			loanOrderInfo.setPeroid(order.getPeroid());
			loanOrderInfo.setSourceId(order.getSourceId());
			loanOrderInfo.setLoanAmount(order.getLoanAmount());
			loanOrderInfo.setLoanTime(order.getLoanTime());
			loanOrderInfo.setSettleAmount(order.getSettleAmount());
			loanOrderInfo.setTransactionId(order.getTransactionId());
			loanOrderInfo.setTransactionTime(order.getTransactionTime());
			loanOrderInfo.setAuthorize(order.getAuthorize());
			loanOrderInfo.setAuthTime(order.getAuthTime());
			loanOrderInfo.setSourceUrl(order.getSourceUrl());
			loanOrderInfo.setBorrowerId(order.getBorrowerId());
			loanOrderInfo.setLoanAppId(order.getLoanAppId());
			loanOrderInfo.setPhoneId(order.getPhoneId());
			loanOrderInfo.setPlatform(order.getPlatform());
			loanOrderInfo.setProductId(order.getProductId());
			loanOrderInfo.setChannel(order.getChannel());
			loanOrderInfo.setCtime(order.getCtime());
			loanOrderInfo.setMemo(order.getMemo());
			loanOrderInfo.setRecordView(structureLoanApiRequestRecordView(orderId));
		}
		return loanOrderInfo;
	}

	@Override
	public Page<LoanApiOrderSearchResponse> list(PageSearcher<LoanApiOrderSearchRequest> pageSearcher) {
		return loanApiOrderRepository.list(pageSearcher.getSearchCondition(), pageSearcher.getPageable());
	}
	
	/**
	 * 查询订单请求记录
	 * @param orderId
	 * @return
	 */
	private LoanApiRequestRecordView structureLoanApiRequestRecordView(Long orderId) {
        
		LoanApiRequestRecordView view = new LoanApiRequestRecordView();

		List<LoanApiRequestRecordView.LoanApiRequestRecord> records = requestRecordRepository.findAllByOrderId(orderId)
				.stream().sorted(Comparator.comparing(LoanApiRequestRecord::getCreateTime).reversed()).map(x -> {

					LoanApiRequestRecordView.LoanApiRequestRecord record = new LoanApiRequestRecordView.LoanApiRequestRecord();
					BeanUtils.copyProperties(x, record);
					return record;

				}).collect(Collectors.toList());
		view.setRecords(records);
		return view;
	}
}
